using System.Collections;
using Bullet;
using UnityEngine;
using Weapon;

namespace Enemy
{
    /// <summary>
    /// 原地不动投掷炸弹的敌人
    /// </summary>
    public class ProjectileEnemy : BaseEnemy
    {
        [SerializeField] public float offset;//敌人导弹位置向上偏移量
        private void Start()
        {
            /*            MaxHp = 150;
                        Hp = MaxHp;
                        AttackRange = 50;
                        experienceAmount = 120;*/

            Hp = MaxHp;
            bulletPrefab = Resources.Load<GameObject>("Prefabs/Enemy/Bullet/Projectile");
            base.Start();
        }

        private void Update()
        {
            //Action();
            base.Update();
        }

        public override void Action()
        {

            // 面向敌人方向，只改变Y轴旋转
            if (Player.Player.instance != null)
            {
                Vector3 targetDirection = Player.Player.instance.transform.position - transform.position;
                targetDirection.y = 0; // 忽略Y轴方向

                Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
                transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, targetRotation.eulerAngles.y, transform.rotation.eulerAngles.z);
            }
            
            //发射子弹
            if(Player.Player.instance != null)
            {
                if(Time.time >= nextAttackTime)
                {
                    CreateBullet();
                    nextAttackTime = Time.time + attackCooldown;
                    Debug.Log("创建子弹");
                }
            }
        }
        

        /// <summary>
        /// 创建敌人子弹
        /// </summary>
        private void CreateBullet()
        {
            //利用对象池来管理敌人子弹
            GameObject bullet = PoolManager.Release(bulletPrefab, transform.position + Vector3.up * offset, transform.rotation);
            bullet.GetComponent<ProjectileBullet>().attackRange = AttackRange;
        }
        
        /// <summary>
        /// 玩家子弹碰撞
        /// </summary>
        /// <param name="other"></param>
        public void OnTriggerEnter(Collider other)
        {
            base.OnTriggerEnter(other);
            if (other.gameObject.CompareTag("Bullet"))
            {
                GetHit();
            }
        }
    }
}
